package com.yisu.auction.manager.core.service.impl;

import com.yisu.auction.ewf.entity.RechargeableCard;
import com.yisu.auction.manager.core.mapper.RechargeableCardMapper;
import com.yisu.auction.manager.core.service.IRechargeableCardService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * author: 赵杰
 * Business Description：
 * createTime：2017/9/10
 */
@Service
public class RechargeableCardServiceImpl implements IRechargeableCardService {
    @Resource
    private RechargeableCardMapper rechargeableCardMapper;

    /**
     * 批量添加充值卡
     *
     * @param rechargeableCards
     * @return
     */
    @Override
    public boolean bathAddModel(List<RechargeableCard> rechargeableCards) {
        if (!CollectionUtils.isEmpty(rechargeableCards)) {
            for (RechargeableCard insertModel : rechargeableCards) {
                insertModel.setIsValid(true);
                insertModel.setCreateDate(new Date());
                rechargeableCardMapper.insertSelective(insertModel);
            }

        }
        return true;
    }

    /**
     * 通过prizeId 获取充值卡列表
     *
     * @param prizeId
     * @return
     */
    @Override
    public List<RechargeableCard> getListByPrizeId(Long prizeId) {
        return rechargeableCardMapper.getListByPrizeId(prizeId);
    }


    /**
     * author:黄萧林
     * 添加充值卡信息
     *
     * @param rechargeableCard
     * @return
     */
    @Override
    public boolean modifyRechargeCard(RechargeableCard rechargeableCard) {
        RechargeableCard entity = rechargeableCardMapper.selectByPrimaryKey(rechargeableCard.getId());
        if (entity != null) {
            entity.setUpdateDate(new Date());
            entity.setId(entity.getId());
            entity.setCardValue(rechargeableCard.getCardValue());
            entity.setCardNo(rechargeableCard.getCardNo());
            entity.setCardPwd(rechargeableCard.getCardPwd());
            entity.setBillType(rechargeableCard.getBillType());
            entity.setPrizeId(rechargeableCard.getPrizeId());
            return rechargeableCardMapper.updateByPrimaryKeySelective(entity) > 0;
        } else {
            RechargeableCard insertRechargeableCard = new RechargeableCard();
            insertRechargeableCard.setCreateDate(insertRechargeableCard.getCreateDate());
            insertRechargeableCard.setCreateDate(new Date());
            insertRechargeableCard.setPrizeId(rechargeableCard.getPrizeId());
            insertRechargeableCard.setCardValue(rechargeableCard.getCardValue());
            insertRechargeableCard.setIsValid(true);
            insertRechargeableCard.setCardNo(rechargeableCard.getCardNo());
            insertRechargeableCard.setCardPwd(rechargeableCard.getCardPwd());
            insertRechargeableCard.setBillType(rechargeableCard.getBillType());
            return rechargeableCardMapper.insertSelective(insertRechargeableCard) > 0;
        }
    }

    /**
     * author:黄萧林
     * 删除充值卡信息
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteRechargeCard(Long id) {
        return rechargeableCardMapper.deleteByPrimaryKey(id) > 0;
    }


    /**
     * 查询一条数据
     * @param id
     * @return
     */
    @Override
    public RechargeableCard getRechargeableCard(Long id) {
        return rechargeableCardMapper.selectByPrimaryKey(id);
    }
}
